﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Goods.Entity;
using NewRetail.Application.Components.PrintTask;
using NewRetail.Application.Components.PrintTask.Entity;
using NewRetail.Application.Components.PrintTask.Model;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using NewRetail.Application.Core.Entity;
using NewRetail.Application.Utility;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("print-task")]
    public class PrintTaskController : BaseController<PrintTaskEntity>
    {
        private readonly IPrintTaskService printTaskService;

        public PrintTaskController(IPrintTaskService printTaskService)
        {
            this.printTaskService = printTaskService;
        }

        #region 根据仓库Id查询所有的打印任务
        [HttpPost("query-all-need-print")]
        public async Task<ResponseEntity<PrintTaskDataResultDto[]>> QueryByReceiptNo([FromBody] PrintTaskQueryAllInputDto dto)
        {
            dto.Should().NotBeNull();
            dto.warehouseId.Should().NotBeNullOrEmpty();
            var db = await this.Repo.GetDbContextAsync();
            //查询采购单
            var inOutWarehousings = await db.Set<InOutWarehousing>().AsNoTracking().Where(x => x.warehouseID == dto.warehouseId).ToArrayAsync();
            var receiptNos = inOutWarehousings.Select(x => x.receiptNo);
            
            //查询调价单
            var goodsAdjustPrices = await db.Set<GoodsAdjustPrice>().AsNoTracking().Where(x => x.warehouseID == dto.warehouseId).ToArrayAsync();
            var adjustPriceReceiptNos = goodsAdjustPrices.Select(x => x.receiptNo);

            //查询其他单
            var otherBills = await db.Set<OtherBill>().AsNoTracking().Where(x => x.warehouseID == dto.warehouseId).ToArrayAsync();
            var otherBillReceiptNos = otherBills.Select(x => x.receiptNo);

            var allReceiptNos = receiptNos?.Union(adjustPriceReceiptNos)?.Union(otherBillReceiptNos);
            var query = this.SingleStoreService.ApplyToQueryable(this.Repo);
            //var data = await query.Where(x => x.printTag == 0 && receiptNos != null && receiptNos.Contains(x.receiptNo)).ToArrayAsync();
            var data = await query.Where(x => x.printTag == 0 && allReceiptNos != null && allReceiptNos.Contains(x.receiptNo)).ToArrayAsync();

            var result = await this.printTaskService.AttachGoodsInfo(data, dto.warehouseId);

            return new ResponseEntity<PrintTaskDataResultDto[]>().SetData(result);
        }
        #endregion

        #region 更新打印任务的记录状态，将0变成1
        [HttpPost("update-printtag")]
        public async Task<ResponseEntity<string>> UpdatePrintTagByReceiptNo([FromBody] ReceiptNoDto dto)
        {
            dto.Should().NotBeNull();
            dto.receiptNo.Should().NotBeNullOrEmpty();
            var ret = new ResponseEntity<string>();
            var result = await this.printTaskService.UpdatePrintTagByReceiptNo(dto);

            if (string.IsNullOrEmpty(result.Item1))
                ret.SetError("更新失败！" + result.Item2);
            else
                ret.SetData("更新成功");

            return ret;
        }
        #endregion
    }
}
